package org.example.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 专门用来连接数据库的
 */
public class ConnectionDB {
    // 创建读取属性文件对象
    static Properties properties = new Properties();
    // 保存从属性文件当中获取的连接字符串
    static String url;
    static String userName;
    static String password;

    static {
        InputStream is = ConnectionDB.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(is);
            url = (String)properties.get("url");
            userName = (String)properties.get("username");
            password = properties.get("password").toString();
        } catch (IOException e) {
            e.printStackTrace();  // 显示异常对战，并且显示异常真正的原因
            System.out.println("连接数据库异常");
        }
    }

    public static void main(String[] args) {

//        String url = "jdbc:mysql://localhost:3306/db_company?useSSL=false&serverTimezone=UTC";
//        String userName = "root";
//        String password = "root";
        // DriverManager  是一个静态的工具类
        try {
            // Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url,userName,password);
            if(conn != null) {
                System.out.println("连接成功！");
            }
        } catch (SQLException e) {
            System.out.println("连接数据库异常");
       }//  catch (ClassNotFoundException e) {
//            System.out.println("加载类异常，被加载的类不存在");
//        }
    }
}
